package sgi.client;
import java.awt.BorderLayout;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;

import sgi.details_new.ComplaintDetails;

public class SGIComplaintReviewUI extends SGIClientFrame {
	private JPanel contentPane;
	private JScrollPane scrollPane;
	private JTable table;
	private static SGIComplaintReviewController ReviewController;
	private JTextField compField;
	public SGIComplaintReviewUI() 
	{
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 659, 525);		
		
		setTitle("Complaints table");
		contentPane = new JPanel();
		getContentPane().setLayout(null);
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		/** return to user accout menu UI */
		JButton btnReturn = new JButton("Return");
		btnReturn.setBounds(10, 450, 123, 36);
		getContentPane().add(btnReturn);
		btnReturn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				SGIComplaintReviewController.terminate();
				SGIUserAccountMenuController.create(null);
			}
		});
		/** button to decline the complaint */
		JButton btnDecline = new JButton("Decline");
		btnDecline.setBounds(216, 463, 118, 23);
		contentPane.add(btnDecline);
		btnDecline.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					int selects = table.getSelectedRow();
			        String userName=(String) table.getValueAt(selects, 0);
			        String DateTime=(String) table.getValueAt(selects, 1);
					ReviewController.handleDeclineMessageFromComplaintReviewUI(userName,DateTime);
					review();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		});
		/** textfield of the compensation to give if accepted */
		JLabel lblCompansation = new JLabel("Compensation :");
		lblCompansation.setBounds(344, 467, 100, 14);
		contentPane.add(lblCompansation);
		
		compField = new JTextField();
		compField.setBounds(440, 464, 100, 20);
		contentPane.add(compField);
		compField.setColumns(10);
		
		/** table header */
		Object[] columnNames = {"UserName","DateTime","Type","Title","Description"};
		Object rowData[][]={};
		DefaultTableModel dtm = new DefaultTableModel(rowData, columnNames){
	      public boolean isCellEditable(int rowIndex, int mColIndex) {
	          return false;
	        }
	      };
		table = new JTable(dtm);
		table.setBounds(10, 127, 633, 321);
		
		JTableHeader header = table.getTableHeader();
		
		JPanel panel = new JPanel();
		panel.setBounds(10, 107, 633, 332);
		panel.setLayout(new BorderLayout());
		panel.add(header, BorderLayout.NORTH);
		panel.add(table, BorderLayout.CENTER);

		getContentPane().add(panel);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		panel.add(table);
		
		/** button that if accepts, sending message with compensation field */
		JButton btnAccept = new JButton("Accept");
		btnAccept.setBounds(550, 463, 93, 23);
		btnAccept.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					int sel = table.getSelectedRow();
			        String userName=(String) table.getValueAt(sel, 0);
			        String DateTime=(String) table.getValueAt(sel, 1);
			        if(compField.getText().equals(""))
			        	ReviewController.handleMessageFromComplaintReviewUI(userName,0,DateTime);
			        else
			        	ReviewController.handleMessageFromComplaintReviewUI(userName,(Integer.valueOf(compField.getText())),DateTime);
			        review();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		});
		getContentPane().add(btnAccept);
		

		
		/** arranging columns */
		TableColumn column=null;
		for(int i=0;i<table.getColumnCount(); i++)
		{
			column= table.getColumnModel().getColumn(i);
			if(i== 3)
			{

				column.setWidth(200);
			}
			if(i== 4)
			{

				column.setWidth(600);
			}
		}
		

	}

	/** setting the controller */
	public void setComplaintReviewController(SGIComplaintReviewController review)
	{
		SGIComplaintReviewUI.ReviewController = review;
	}
	/** adding rows to the table */
	public void addRows(ComplaintDetails cmd)
	{
		DefaultTableModel dtm = (DefaultTableModel) table.getModel();
		dtm.insertRow(table.getRowCount(), new Object[] {cmd.getuserName(),cmd.getDate(),String.valueOf(cmd.getType()),cmd.getTitle(),cmd.getDescription()});
		table.setModel(dtm);
	}
	/** clearing the table */
	public void clearTable()
	{
		DefaultTableModel dtm = (DefaultTableModel) table.getModel();
		while(dtm.getRowCount() > 0) {dtm.removeRow(0);}
	}	
	/** reviewing the table UI */
	public void review()
	{
		SGIComplaintReviewController.complaintReviewUI.clearTable();
		ReviewController.ReviewComplaint();	
	}

}
